package day_240424.rps;

import java.util.Random;
import java.util.Scanner;

public class Rps {
    String[] rps = new String[]{"rock", "paper", "scissor"};
    String input;
    int result;
    int count = 0;
    int flag = 0;

    public void userInput() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入 rock、paper or scissor（无关大小写）");
        System.out.println("你目前共有" + (5 - count) + "次机会:");
        input = scanner.next().toLowerCase();
        computerInput();
    }

    public void computerInput() {
        Random random = new Random();
        result = random.nextInt(3);
    }

    public void play(){
        userInput();
        if( (input.equals("scissor") || input.equals("rock") || input.equals("paper") )&& (count<=5)){
            System.out.println("电脑出的是" + rps[result]);

            String[] results = {"平局", "你赢了!", "你输了哦..."};
            int playerIndex = input.equals("rock") ? 0 : input.equals("paper") ? 1 : 2;
            // 这行代码可以看作是下面这段代码的简写形式：(连续使用了俩次三元运算)

            // int playerIndex;
            // if (input.equals("rock")) {
            //     playerIndex = 0;
            // } else if (input.equals("paper")) {
            //     playerIndex = 1;
            // } else {
            //     playerIndex = 2;
            // }

            System.out.println(results[(3 + playerIndex - result) % 3]);
            if( ( (3 + playerIndex - result) % 3)==1){
                flag++;
            }
            if (++count<5){
                play();
            }
            else {
                gameOver();
            }
            // 我们首先计算了玩家和电脑选择之间的差值，然后加上3是为了确保结果始终为正数。相当于对余数取模
        }

        else {
            System.out.println("非法输入！");
            count++;
            if (count < 5) {
                play();
            }
            else {
                gameOver();
            }
        }
    }

    public void gameOver(){
        if (flag >= 3){
            System.out.println("WIN!");
        }
        else {
            System.out.println("GAME-OVER!");
        }
    }
}
